<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class AutogeneratorViewMvc extends JViewLegacy {

    protected $form;
    protected $item;
    protected $state;
    protected $sContent;

    public function display($tpl = null) {
        $this->form = $this->get('Form');
        $this->item = $this->get('Item');
        $this->state = $this->get('State');

        $this->addToolbar();

        parent::display($tpl);
    }

    protected function addToolbar() {
        $oApp = JFactory::getApplication();
        $oApp->input->set('hidemainmenu', true);
        $sLayout = $oApp->input->get('layout');
        $sFile = $oApp->input->get('file');
        $sType = $oApp->input->get('type', 'list');
        $bIsAdmin = $oApp->input->getBool('bIsAdmin', true);
        $iMvcId = $oApp->input->getInt('id');
        
        switch ($sLayout) {
            case 'form':
                JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_MVC_FORM'), 'article.png');
                break;
            case 'list':
                JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_MVC_LIST'), 'article.png');
                break;
            case 'item':
                JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_MVC_ITEM'), 'article.png');
                break;
            case 'review':
                switch ($sFile) {
                    case 'view.html.php':
                        if ($bIsAdmin) {
                            switch ($sType) {
                                case 'list':
                                    $this->sContent = AutogeneratorHelper::getAdminListViewHTMLContent($iMvcId);
                                    break;
                                
                                case 'form':
                                    $this->sContent = AutogeneratorHelper::getAdminFormViewHTMLContent($iMvcId);
                                    break;
                                
                                default:
                                    break;
                            }
                        } else {
                            switch ($sType) {
                                case 'list':
                                    $this->sContent = AutogeneratorHelper::getFrontendListViewHTMLContent($iMvcId);
                                    break;
                                
                                case 'item':
                                    $this->sContent = AutogeneratorHelper::getFrontendItemViewHTMLContent($iMvcId);
                                    break;
                                
                                default:
                                    break;
                            }
                        }
                        break;

                    case 'default.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminListDefaultContent($iMvcId);
                        } else {
                            $this->sContent = AutogeneratorHelper::getFrontendItemDefaultContent($iMvcId);
                        }
                        
                        break;
                    
                    case 'edit.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminFormEditContent($iMvcId);
                        } else {
                            switch ($sType) {
                                case 'list':
                                    break;
                                
                                case 'item':
                                    break;
                                
                                default:
                                    break;
                            }
                        }
                        break;
                    
                    case 'model.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminModelContent($iMvcId);
                        } else {
                            $this->sContent = AutogeneratorHelper::getFrontendModelContent($iMvcId);
                        }
                            
                        break;
                    
                    case 'models.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminModelsContent($iMvcId);
                        } else {
                            $this->sContent = AutogeneratorHelper::getFrontendModelsContent($iMvcId);
                        }
                        
                        break;
                        
                    case 'form.xml':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminXMLFormContent($iMvcId);
                        } else {
                            
                        }
                        break;
                        
                    case 'controller.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminControllerContent($iMvcId);
                        } else {
                            $this->sContent = AutogeneratorHelper::getFrontendControllerContent($iMvcId);
                        }
                        break;
                        
                    case 'controllers.php':
                        if ($bIsAdmin) {
                            $this->sContent = AutogeneratorHelper::getAdminControllersContent($iMvcId);
                        } else {
                            $this->sContent = AutogeneratorHelper::getFrontendControllersContent($iMvcId);
                        }
                        break;
                        
                    default:
                        break;
                }
                break;
            default:
                JToolbarHelper::title(JText::_('COM_AUTOGENERATOR_MVC_DETAILS'), 'article.png');
                break;
        }
        
        if ($sLayout == 'edit') {
            JToolbarHelper::apply('mvc.apply');
            JToolbarHelper::save('mvc.save');
            JToolbarHelper::save2new('mvc.save2new');
        }

        JToolbarHelper::cancel('mvc.cancel');
    }

}
